Prototype Testing

pacman::p_load(sf, tmap, tidyverse,readxl)
also installing the dependencies 'logger', 'geometries', 'rapidjsonr', 'sfheaders', 'terra', 'stringdist', 'spacesXYZ', 'geojsonsf', 'raster', 'jsonify', 'leaflet.providers', 'sp', 'lwgeom', 'dichromat', 'cols4all', 'leafem', 'leafgl', 'leaflegend', 'leaflet', 'leafsync', 'stars', 'tmaptools', 'servr'

The downloaded binary packages are in
    /var/folders/v7/kn7_1jkd60l80zw7rs_11qk00000gn/T//RtmplBT8Ne/downloaded_packages

tmap installed
mpsz <- st_read(dsn = "data/geospatial", 
                layer = "MP14_PLNG_AREA_WEB_PL")
Reading layer `MP14_PLNG_AREA_WEB_PL' from data source 
  `/Users/geloliu/Gelo-608/ISSS608/ISSS608-VAA-GroupProject/ShinyApp/data/geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 55 features and 12 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
age <- read_csv("data/ResidentPopulationbyPlanningAreaSubzoneofResidenceAgeGroupandFloorAreaofResidenceCensusofPopulation2020.csv")
Rows: 388 Columns: 121
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (121): Number, Total1_Total, Total1_0_4, Total1_5_9, Total1_10_14, Total...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
income <- read_excel("data/ResidentHouseholdsbyPlanningAreaofResidenceandMonthlyHouseholdIncomefromWorkCensusOfPopulation2020.xlsx",sheet = "sheet1",range="A11:U43")
age <- age %>%
  filter(grepl("Total", Number, ignore.case = TRUE)) %>%  
  select(1:21) %>%
  replace(. == "-", NA) %>%
  mutate(across(2:21, as.numeric)) %>%
  mutate(Aged = rowSums(select(., 16:21), na.rm = TRUE)) %>%
  mutate(PA = sub(" - Total.*", "", Number))%>%
  select(c("PA","Aged","Total1_Total"))%>%
  mutate(`Aged%` = round(Aged/Total1_Total*100,0),
         PA = toupper(PA))
age <- left_join(mpsz,age,by = c("PLN_AREA_N" = "PA"))
income <- income %>%
  mutate(across(2:21, as.numeric))
income_proportion <- income %>%
  mutate(across(3:21, ~ round(. / income[[2]] * 100, 2))) %>%
  mutate(`LowerIncome%`=rowSums(select(.,3:6), na.rm = TRUE))
low_income <- income_proportion %>%
  select(c(`Planning Area of Residence`,`LowerIncome%`))%>%
  mutate(`Planning Area of Residence` = toupper(`Planning Area of Residence`))
low_income <- left_join(mpsz,low_income,by = c("PLN_AREA_N" = "Planning Area of Residence"))
# Ensure both layers have the same CRS
low_income <- st_transform(low_income, st_crs(age))

# Set the tmap mode to view (interactive)
tmap_mode("view")
ℹ tmap mode set to "view".
# Create the map with multiple layers
tm_shape(age) +
  tm_fill(col = "Aged%", 
          palette = "Blues", 
          title = "Aged Population(%)",
          alpha = 0.9,
          style = "quantile") +
  tm_borders(col = "white", lwd = 0.5, alpha = 0.5) +
tm_shape(low_income) + 
  tm_fill(col = "LowerIncome%", 
          palette = "YlOrRd", 
          title = "IncomeBelow:3000(%)", 
          style = "quantile",
          n = 5,
          alpha = 0.6) +
  tm_borders(col = "darkgrey", lwd = 0.5, alpha = 0.5) +
tm_basemap(server = "CartoDB.Positron") +
tm_layout(title = "Aged and Low-Income Population Across Singapore", 
          legend.outside = TRUE,
          legend.outside.position = "right")

── tmap v3 code detected ───────────────────────────────────────────────────────
[v3->v4] `tm_polygons()`: instead of `style = "quantile"`, use fill.scale =
`tm_scale_intervals()`.
ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
  'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: use 'fill' for the fill color of polygons/symbols
(instead of 'col'), and 'col' for the outlines (instead of 'border.col').[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_borders()`: use `fill_alpha` instead of `alpha`.[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_borders()`: use `fill_alpha` instead of `alpha`.[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "Blues" is named
"brewer.blues"Multiple palettes called "blues" found: "brewer.blues", "matplotlib.blues". The first one, "brewer.blues", is returned.
[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "YlOrRd" is named
"brewer.yl_or_rd"Multiple palettes called "yl_or_rd" found: "brewer.yl_or_rd", "matplotlib.yl_or_rd". The first one, "brewer.yl_or_rd", is returned.
<<<<<<< HEAD
=======
>>>>>>> c2b3c32 (Proposal V2)
pacman::p_load(ggdist,ggridges,lubridate,knitr)
station<-read_csv("data/Changi&MarinaBarrage.csv")
Rows: 8098 Columns: 16
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (13): station, daily_rainfall_total_mm, highest_30_min_rainfall_mm, high...
dbl  (3): year, month, day

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
<<<<<<< HEAD
station <- station %>%
  select(1:13) %>%
  mutate(
    across(5:13, as.numeric),  # Convert only numeric columns
    date = make_date(year, month, day),
    station = as.factor(station)
  )
=======
station <- station %>%
  mutate(mean_temperature_c = ifelse(!is.na(mean_temperature_c), 
                                     mean_temperature_c, 
                                     mean_temperature_a_c),
    maximum_temperature_c = ifelse(!is.na(maximum_temperature_c),
                     maximum_temperature_c, 
                     maximum_temperature_a_c),
    minimum_temperature_c = ifelse(!is.na(minimum_temperature_c), 
                     minimum_temperature_c, 
                     minimum_temperature_a_c),
                     across(5:16, as.numeric,na.rm = TRUE),
    date = make_date(year, month, day),
    station = as.factor(station))
>>>>>>> c2b3c32 (Proposal V2)
Warning: There were 13 warnings in `mutate()`.
The first warning was:
ℹ In argument: `across(5:16, as.numeric, na.rm = TRUE)`.
Caused by warning:
! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
Supply arguments directly to `.fns` through an anonymous function instead.

  # Previously
  across(a:b, mean, na.rm = TRUE)

  # Now
  across(a:b, \(x) mean(x, na.rm = TRUE))
ℹ Run `dplyr::last_dplyr_warnings()` to see the 12 remaining warnings.
ggplot(station, 
       aes(x = mean_temperature_c, y = station, fill = station)) +
  geom_density_ridges(
    scale = 2, 
    rel_min_height = 0.01, 
    alpha = 0.5
  ) +
  labs(title = "Distribution of Temperature Across Stations",
       x = "Mean Temperature (°C)", 
       y = "Station") +
  theme(
    panel.background = element_rect(fill = "#f3f1e9"),
    plot.background = element_rect(fill = "#f3f1e9", color = NA),
    legend.position = "none",
    plot.title = element_text(face = "bold")
  )
Picking joint bandwidth of 0.204
Warning: Removed 1279 rows containing non-finite outside the scale range
(`stat_density_ridges()`).

ggplot(station, 
       aes(x = station,y = mean_temperature_c)) +
  stat_halfeye(
               alpha = 0.5,
               adjust = 0.5,
               justification = -0.1,
               .width = 0,
               fill = "#8AA4FF")+
  geom_boxplot(width = 0.10,
               outlier.shape = NA,
               color="grey50")+
  labs(title ="Distribution of Mean Temperature Across Stations",
       x = "", y="Mean Temperature")+
  coord_flip() +
  theme(panel.background = element_rect(fill = "#ffffff"),
        plot.background = element_rect(fill = "#ffffff",color = NA),
        legend.position = 'none',
        plot.title = element_text(face = "bold",size=13,hjust=0.5))
Warning: Removed 1279 rows containing missing values or values outside the scale range
(`stat_slabinterval()`).
Warning: Removed 1279 rows containing non-finite outside the scale range
(`stat_boxplot()`).

quartile_station_table <- station %>%
  group_by(station) %>%
  summarize(
    Min    = round(min(mean_temperature_c, na.rm = TRUE),2),
    Q1     = round(quantile(mean_temperature_c, probs = 0.25, na.rm = TRUE),2),
    Median = round(median(mean_temperature_c, na.rm = TRUE),2),
    Mean   = round(mean(mean_temperature_c, na.rm = TRUE),2),
    Q3     = round(quantile(mean_temperature_c, probs = 0.75, na.rm = TRUE),2),
    Q4     = round(quantile(mean_temperature_c, probs = 1, na.rm = TRUE),2),
    Max    = round(max(mean_temperature_c, na.rm = TRUE),2)
  )

kable(quartile_station_table)
station Min Q1 Median Mean Q3 Q4 Max
Changi 23.0 27.3 28.2 28.08 28.9 30.7 30.7
Marina Barrage 23.2 27.8 28.7 28.61 29.5 31.3 31.3
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(ggplot2)
library(dplyr)

# Change to "all" to display all stations
selected_station <- c("Changi", "Marina Barrage")  

# Filter the data accordingly
station_data <- if ("all" %in% selected_station) {
  station
} else {
  station %>% filter(station %in% selected_station)
}

# Create the plot using the filtered data
p <- ggplot(data = station_data, 
            aes(x = mean_temperature_c,
                y = daily_rainfall_total_mm, 
                color = station)) +
  geom_point(size = 1, alpha = 0.7) +  
  coord_cartesian(ylim = c(0, 150))+
  theme_minimal() +
  labs(x = "Mean Temperature (°C)", y = "Daily Total Rainfall (mm)") +
  theme(
    plot.background = element_rect(fill = "white", color = NA),
    panel.background = element_rect(fill = "white"),
    axis.title = element_text(size = 10, hjust = 0.5),
    axis.text = element_text(size = 8),
    legend.position = "top",
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5)
  )


ggplotly(p)
<<<<<<< HEAD
=======
library(dplyr)

# Replace missing values in mean_temperature_c, Maximum, and Minimum
station <- station %>%
  mutate(
    mean_temperature_c = ifelse(!is.na(mean_temperature_c), mean_temperature_c, mean_temperature_a_c),
    Maximum = ifelse(!is.na(maximum_temperature_c), maximum_temperature_c, max( maximum_temperature_a_c, na.rm = TRUE)),
    Minimum = ifelse(!is.na(minimum_temperature_c), minimum_temperature_c, min(minimum_temperature_a_c, na.rm = TRUE))
  )
changi_data <- subset(station, station == "Changi")

ggplot(changi_data, aes(x = date, y = mean_wind_speed_km_h, color = station, group = station)) +
  geom_line(size = 1, alpha = 0.8) +
  theme_minimal() +
  labs(title = "Mean Temperature Over Time by Station",
       x = "Date",
       y = "Mean Temperature (°C)") +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    legend.title = element_blank(),
    legend.position = "top"
  )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

>>>>>>> c2b3c32 (Proposal V2)